package com.zs.studentmanager.controller;

import com.zs.studentmanager.common.ResponseCodeEnum;
import com.zs.studentmanager.common.Result;
import com.zs.studentmanager.entity.vo.PageVo;
import com.zs.studentmanager.entity.vo.TeacherVo;
import com.zs.studentmanager.service.TeacherService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * @author: 周小白
 * @date: 2024-01-22 19:40
 **/
@RestController
@RequestMapping("/teacher")
@Tag(name = "教师管理")
public class TeacherController {

    private final TeacherService teacherService;

    public TeacherController(TeacherService teacherService) {
        this.teacherService = teacherService;
    }





    /**
     * 获取教师列表
     * @param pageNum 当前页
     * @param pageSize 每页显示条数
     * @param searchValue 搜索值
     * @return 教师列表
     */
    @GetMapping("/list")
    @Operation(summary = "获取教师列表")
    public Result getTeacherList(@RequestParam(value = "pageNum") Integer pageNum,
                                 @RequestParam(value = "pageSize") Integer pageSize,
                                 @RequestParam(value = "searchValue", required = false) String searchValue) {
        if (pageNum == null || pageSize == null) {
            return Result.fail(ResponseCodeEnum.NO_PARAM);
        }
        if (pageNum <= 0 || pageSize <= 0) {
            return Result.fail("参数错误");
        }

        PageVo<TeacherVo> pageVo = teacherService.getTeacherList(pageNum, pageSize, searchValue);
        if (pageVo == null) {
            return Result.fail(ResponseCodeEnum.SYSTEM_ERROR);
        }

        return Result.success(pageVo);
    }







    /**
     * 添加教师
     * @param teacherVo 教师信息
     * @return 添加结果
     */
    @PostMapping("/add")
    @Operation(summary = "添加教师")
    @Transactional(rollbackFor = Exception.class)
    public Result addTeacher(@RequestBody TeacherVo teacherVo) {
        if (teacherVo == null) {
            return Result.fail(ResponseCodeEnum.NO_PARAM);
        }

        // 添加
        boolean result = teacherService.addTeacher(teacherVo);
        if (!result) {
            return Result.fail(ResponseCodeEnum.SYSTEM_ERROR);
        }

        return Result.success();
    }



    /**
     * 获取教师详情
     * @param id 教师id
     * @return 教师详情
     */
    @GetMapping("/{id}")
    @Operation(summary = "获取教师详情")
    public Result getTeacherDetails(@PathVariable("id") Long id) {
        if (id == null) {
            return Result.fail(ResponseCodeEnum.NO_PARAM);
        }

        TeacherVo teacherVo = teacherService.getTeacherDetails(id);
        if (teacherVo == null) {
            return Result.fail(ResponseCodeEnum.SYSTEM_ERROR);
        }

        return Result.success(teacherVo);
    }





    /**
     * 修改教师信息
     * @param teacherVo 教师信息
     * @return 修改结果
     */
    @PutMapping("/update")
    @Operation(summary = "修改教师信息")
    @Transactional(rollbackFor = Exception.class)
    public Result updateTeacher(@RequestBody TeacherVo teacherVo) {
        if (teacherVo == null) {
            return Result.fail(ResponseCodeEnum.NO_PARAM);
        }

        // 修改
        boolean result = teacherService.updateTeacher(teacherVo);
        if (!result) {
            return Result.fail(ResponseCodeEnum.SYSTEM_ERROR);
        }

        return Result.success();
    }





    /**
     * 删除教师
     * @param id 教师id
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除教师")
    public Result deleteTeacher(@PathVariable("id") Long id) {
        if (id == null) {
            return Result.fail(ResponseCodeEnum.NO_PARAM);
        }

        // 删除
        boolean result = teacherService.removeById(id);
        if (!result) {
            return Result.fail(ResponseCodeEnum.SYSTEM_ERROR);
        }

        return Result.success();
    }



}
